package com.concurrent.syncContainer;

import com.concurrent.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author: zhouyang
 * @descrpition: 在遍历一个容器的时候 标准的for循环是最优的
 * @time: 2019-05-07 13:38
 */
@Slf4j
@ThreadSafe
public class Collection2 {
    private static Vector<Integer> vector = new Vector<>();

    private static void test1(Vector<Integer> vector){
        for(Integer i:vector){
            if(i.equals(3)) vector.remove(i);
        }
    }

    private static void test2(Vector<Integer> vector){
        Iterator<Integer> iterator = vector.iterator();
        while(iterator.hasNext()){
            Integer i = iterator.next();
            if(i.equals(3)) vector.remove(i);
        }
    }

    private static void test3(Vector<Integer> vector){
        for(int i=0;i<vector.size();++i){
            Integer now = vector.get(i);
            if(now.equals(3)) vector.remove(now);
        }
    }

    public static void main(String[] args) {
        vector.add(1);
        vector.add(2);
        vector.add(3);
        //test1(vector);//ConcurrentModificationException
        //test2(vector);//ConcurrentModificationException
        test3(vector);
    }
}
